<template>
	<div>
		<el-steps :active="state.active" v-if="type != 1" align-center>
			<el-step title="项目基本信息" />
			<el-step title="项目阶段" />
			<el-step title="完成" />
		</el-steps>

		<el-form
			v-if="state.active == 1"
			:model="formInline"
			ref="formInlineRef"
			:rules="projectRules"
			class="demo-form-inline"
			label-width="180px"
		>
			<el-row>
				<el-col :span="8">
					<el-form-item label="关联项目集" prop="projectSetId">
						<el-select
							v-model="formInline.projectSetId"
							clearable
							filterable
							:disabled="type == 1"
							placeholder="关联项目集"
						>
							<el-option
								v-for="item in state.projectSetsList"
								:key="item.projectSetId"
								:label="item.projectSetName"
								:value="item.projectSetId"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="项目名称" prop="projectName">
						<el-input
							:disabled="type == 1"
							v-model="formInline.projectName"
							placeholder="请输入项目名称"
						>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="项目编号">
						<el-input disabled v-model="formInline.projectId" placeholder="">
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="项目分类" prop="projectTypeId">
						<el-select
							v-model="formInline.projectTypeId"
							clearable
							:disabled="type == 1"
							placeholder=""
							@change="projectTypeChange"
						>
							<el-option
								v-for="item in state.typeList"
								:key="item.value"
								:label="item.label"
								:value="item.value"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="业务单元" prop="businessUnit">
						<el-select
							v-model="formInline.businessUnit"
							clearable
							filterable
							:disabled="type == 1"
							placeholder="业务单元"
						>
							<el-option
								v-for="(item, index) in businessUnitList"
								:key="index"
								:label="item.businessUnitName"
								:value="item.businessUnitId"
							></el-option>
							<!--
              <el-option
                v-for="item in businessUnitArr"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />-->
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="计划实施时间范围" prop="time">
						<el-date-picker
							v-model="formInline.time"
							@change="handelChangeTime"
							:disabled="type == 1"
							type="datetimerange"
							start-placeholder="开始时间"
							end-placeholder="结束时间"
							format="YYYY-MM-DD"
							value-format="YYYY-MM-DD"
							size="large"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="项目位置" prop="streetCode">
						<el-select
							v-model="formInline.streetCode"
							clearable
							:disabled="type == 1"
							placeholder="项目位置"
							@change="streerChange"
						>
							<el-option
								v-for="item in state.streetList"
								:value="item.value"
								:key="item.value"
								:label="item.label"
							></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="项目详细地址" prop="address">
						<el-input
							:disabled="type == 1"
							v-model="formInline.address"
							placeholder="请输入项目详细地址"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="拟计划总投额" prop="projectAmount">
						<el-input
							:disabled="type == 1"
							v-model="formInline.projectAmount"
							@input="formInline.projectAmount = limitInputNum(formInline.projectAmount,13,4,true)"
							placeholder="请输入拟计划总投额"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="8" v-if="projectType == 3">
					<el-form-item label="项目类型" prop="projectCategory">
						<el-select
							v-model="formInline.projectCategory"
							clearable
							:disabled="type == 1"
							placeholder="项目类型"
							@change=""
						>
							<el-option value="1" label="公司自建"></el-option>
							<el-option value="2" label="政府投资"></el-option>
							<el-option value="3" label="代建项目"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="项目立项部门">
						<el-select
							v-model="formInline.responsibleDepartmentId"
							clearable
							disabled
							placeholder="项目立项部门"
							@change="departmentIdChange(formInline.responsibleDepartmentId)"
						>
							<el-option
								v-for="item in state.allDepartmentList"
								:key="item.departmentId"
								:label="item.departmentName"
								:value="item.departmentId"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="项目负责人" prop="responsibleUserId">
						<el-select
							v-model="formInline.responsibleUserId"
							clearable
							filterable
							:disabled="type == 1"
							placeholder="项目负责人"
						>
							<el-option
								v-for="item in state.departmentPersonList"
								:key="item.userId"
								:label="item.userName"
								:value="item.userId"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<!--        <el-col :span="8">
          <el-form-item label="项目编码" prop="projectCode">
            <el-input
              :disabled="type == 1"
              v-model.number="formInline.projectCode"
              placeholder="请输入项目编码"
            />
          </el-form-item>
        </el-col>-->
				<el-col :span="8">
					<el-form-item label="所属业态" prop="typeOfBusiness">
						<el-select
							v-model="formInline.typeOfBusiness"
							clearable
							:disabled="type == 1"
							placeholder="所属业态"
						>
							<el-option
								v-for="item in state.typeofBusinessList"
								:key="item.value"
								:label="item.label"
								:value="item.value"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="项目性质" prop="projectNature">
						<el-select
							v-model="formInline.projectNature"
							clearable
							placeholder="项目性质"
						>
							<el-option
								v-if="formInline.projectTypeId === '2'"
								v-for="item in state.projectNatureList"
								:key="item.value"
								:label="item.label"
								:value="item.value"
							/>
							<el-option
								v-if="formInline.projectTypeId === '1'"
								v-for="item in state.projectNatureList2"
								:key="item.value"
								:label="item.label"
								:value="item.value"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="年度计划投资额" prop="yearAmount">
						<el-input
							v-model.number="formInline.yearAmount"
							placeholder="请输入年度计划投资额"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="项目新增类型" prop="projectAddType">
						<el-select
							v-model="formInline.projectAddType"
							clearable
							placeholder="项目新增类型"
						>
							<el-option
								v-for="item in state.projectAddTypeList"
								:key="item.value"
								:label="item.label"
								:value="item.value"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<!--          <el-col :span="8">
              <el-form-item label="投资用途" prop="investUse">
                  <el-select
                          v-model="formInline.investUse"
                          clearable
                          placeholder="投资用途"
                  >
                      <el-option
                              v-for="item in state.investUseList"
                              :key="item.value"
                              :label="item.label"
                              :value="item.value"
                      />
                  </el-select>
              </el-form-item>
          </el-col>-->
				<el-col :span="8">
					<el-form-item label="投资内容" prop="investContent">
						<el-select
							v-model="formInline.investContent"
							clearable
							placeholder="投资内容"
						>
							<el-option
								v-for="item in selectionArr"
								:key="item.value"
								:label="item.label"
								:value="item.value"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="投资来源" prop="investSource">
						<el-select
							v-model="formInline.investSource"
							clearable
							placeholder="投资来源"
						>
							<el-option
								v-for="item in state.investSourceList"
								:key="item.value"
								:label="item.label"
								:value="item.value"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="计划状态" prop="planStatus">
						<el-select
							v-model="formInline.planStatus"
							clearable
							placeholder="计划状态"
						>
							<el-option
								v-for="item in state.planStatusList"
								:key="item.value"
								:label="item.label"
								:value="item.value"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<!--          <el-col :span="8">
              <el-form-item label="项目类型" prop="typeId">
                  <el-select
                          v-model="formInline.typeId"
                          clearable
                          placeholder="项目类型"
                          @change="projectTypeChange"
                  >
                      <el-option
                              v-for="item in state.projectTypeIdList"
                              :key="item.value"
                              :label="item.label"
                              :value="item.value"
                      />
                  </el-select>
              </el-form-item>
          </el-col>-->
				<el-col :span="8">
					<el-form-item label="投资类型" prop="investmentType">
						<el-select
							v-model="formInline.investmentType"
							clearable
							placeholder="投资类型"
						>
							<el-option
								v-for="item in state.investmentTypeList"
								:key="item.value"
								:label="item.label"
								:value="item.value"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="行业类型" prop="industryTypeId">
						<el-select
							v-model="formInline.industryTypeId"
							clearable
							filterable
							placeholder="行业类型"
						>
							<el-option
								v-for="item in state.industryTypeIdList"
								:key="item.enumId"
								:label="item.enumName"
								:value="item.enumId"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="实施主体" prop="companyId">
						<el-select
							v-model="formInline.companyId"
							clearable
							filterable
							placeholder="请选择实施主体"
						>
							<el-option
								v-for="item in state.companyList"
								:key="item.companyId"
								:label="item.companyName"
								:value="item.companyId"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item
						:label="formInline.projectTypeId === '1' ? '项目简介' : '项目分析'"
						prop="note"
					>
						<el-input
							v-model="formInline.note"
							show-word-limit
							type="textarea"
							:placeholder="
								formInline.projectTypeId === '1'
									? '请输入项目简介'
									: '请输入项目分析'
							"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="8" v-if="getURLhandler(route.path).type == 1">
					<el-form-item label="项目创建时间" prop="">
						<el-input
							:disabled="true"
							v-model.number="formInline.createTime"
							placeholder=""
						/>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item
						label="项目创建人"
						prop=""
						v-if="getURLhandler(route.path).type == 1"
					>
						<el-input
							:disabled="true"
							v-model.number="formInline.createUserName"
							placeholder=""
						/>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>

		<!--项目阶段-->
		<projectStageTree
			:projectTypeId="formInline.projectTypeId"
			ref="projectStagetree"
			:departmentList="state.allDepartmentList"
			:projectId="getURLhandler(route.path).projectId || state.projectId"
			:startTime="state.startTime"
			:endTime="state.endTime"
			v-if="state.active == 2"
			@getStageTimeChange="getStageTimeChange"
		></projectStageTree>

		<div class="buttons" v-if="state.active !== 3">
			<el-button @click="goback" class="button_cancel2">取消</el-button>
			<el-button
				v-if="type != 1"
				type="primary"
				@click="submitHandler(formInlineRef)"
        class="button_edit"
			>
				下一步
			</el-button>
			<el-button
				type="primary"
				v-if="type == 1 && formInline?.examineStatus == '0'"
				@click="type = 2"
        class="button_edit"
			>
				编辑
			</el-button>
		</div>

		<div v-if="state.active == 3">
			<p style="height: 40px">
				<svg-icon
					icon-class="sucesss-icon"
					style="width: 100%; height: 100%"
				></svg-icon>
			</p>
			<p style="font-size: 16px; text-align: center">
				{{
					`恭喜您，完成项目集的${
						getURLhandler(route.path).type ? "编辑" : "添加"
					}!`
				}}
			</p>
			<p style="font-size: 16px; text-align: center">
				<el-button @click="goback" class="button_edit">返回列表页</el-button>
			</p>
		</div>
		<mapDialog ref="mapdialog" @getMapAddrInfo="getMapAddrInfo"></mapDialog>
	</div>
</template>
<script setup>
import { getURLhandler } from "@/views/constructionProjectManage/static/utools.js";
import "@/views/constructionProjectManage/static/button.scss";
import MapDialog from "@/views/constructionProjectManage/projectmanagement/components/mapDialog.vue";
import { ref, reactive, onMounted, computed, watch } from "vue";
import { assetsApi } from "@/api/assets";
import { useRouter, useRoute } from "vue-router";
import { businessUnitArr } from "@/common/dict/dictCollect.js";
import ProjectStageTree from "@/views/constructionProjectManage/preliminaryProject/components/projectStageTree.vue";

import {
	projectApi,
	projectListApi,
	department,
	publicProject,
	projectConfigApi,
	projectPhase,
} from "@/api/project";
import { fundManagementApi } from "@/api/businessFinance";
import PublicDialog from "@/components/PublicDialog.vue";
import {
	ElTag,
	ElForm,
	ElSelect,
	ElFormItem,
	ElStep,
	ElButton,
	ElTree,
	ElSteps,
	ElDatePicker,
	ElMessage,
	ElRow,
	ElCol,
	ElOption,
} from "element-plus";
import { investmentProjects, otherApi } from "@/api/investment";
import {limitInputNum, localGet} from "@/common/utils/GlobalUtils";
const projectStagetree = ref(null);
const dialogForm = reactive({
	projectStageId: "",
	projectCode: "",
	projectId: "",
	responsibleDepartmentId: "",
	stageName: "",
	parentProjectStageId: "",
	stageOrder: "",
	level: "",
});
const state = reactive({
	startTime:'',
	endTime:'',
	companyList: [],
	streetList: [],
	projectNatureList: [
		{
			label: "新建项目",
			value: "1",
		},
		{
			label: "续建项目",
			value: "2",
		},
	],
	projectNatureList2: [
		{
			label: "新设立",
			value: "3",
		},
		{
			label: "续投",
			value: "4",
		},
	],
	projectAddTypeList: [
		{
			label: "年初计划",
			value: 1,
		},
		{
			label: "年内新增",
			value: 2,
		},
	],
	investUseList: [
		{
			label: "经营",
			value: "1",
		},
		{
			label: "管理",
			value: "2",
		},
		{
			label: "其他",
			value: "3",
		},
	],
	// investContentList: [
	// 	{
	// 		label: "建设",
	// 		value: 1,
	// 	},
	// 	{
	// 		label: "装修",
	// 		value: 2,
	// 	},
	// 	{
	// 		label: "设备",
	// 		value: 3,
	// 	},
	// 	{
	// 		label: "新设",
	// 		value: 4,
	// 	},
	// 	{
	// 		label: "收购",
	// 		value: 5,
	// 	},
	// 	{
	// 		label: "增资",
	// 		value: 6,
	// 	},
	// 	{
	// 		label: "其他",
	// 		value: 7,
	// 	},
	// ],
	investSourceList: [
		{
			label: "政府投资",
			value: 1,
		},
		{
			label: "公司自投",
			value: 2,
		},
		{
			label: "其他",
			value: 3,
		},
	],
	planStatusList: [
		{
			label: "计划外",
			value: -1,
		},
		{
			label: "计划内",
			value: 1,
		},
	],
	projectTypeIdList: [
		{
			label: "股权投资",
			value: "1",
		},
		{
			label: "固定投资",
			value: "2",
		},
	],
	investmentTypeList: [
		{
			label: "全资",
			value: 1,
		},
		{
			label: "控股",
			value: 2,
		},
		{
			label: "参股",
			value: 3,
		},
	],
	industryTypeIdList: [],
	//养老、教育、医疗、农贸、综合体、绿道、文体、子公司、其他
	typeofBusinessList: [
		{
			label: "养老",
			value: 1,
		},
		{
			label: "教育",
			value: 2,
		},
		{
			label: "医疗",
			value: 3,
		},
		{
			label: "农贸",
			value: 4,
		},
		{
			label: "综合体",
			value: 5,
		},
		{
			label: "绿道",
			value: 6,
		},
		{
			label: "文体",
			value: 7,
		},
		{
			label: "子公司",
			value: 8,
		},
		{
			label: "其他",
			value: 9,
		},
	],
	projectId: "",
	projectSetsList: [],
	typeList: [],
	projectSetIdList: [],
	projectCategoryList: [
		{
			label: "公司自建",
			value: "1",
		},
		{
			label: "政府投资",
			value: "2",
		},
		{
			label: "代建项目",
			value: "3",
		},
	],
	allDepartmentList: [],
	departmentPersonList: [],
	userByDepartmentList: [],
	projectTypeList: [],
	streerTypeOptionsArr: [],
	dataSource: [], //树行数据
	projectOptionsArr: [], //所有项目
	active: 1,
	success: false, //编辑或添加是否成功
	type: 0, //0新增 1编辑
});

let centerDialogVisible = ref(false);
let formInlineRef = ref(null);

const formInline = reactive({
	time: [],
	streetName: "",
	projectAmount:'',
	yearAmount:'',
});

//路由跳转
let router = useRouter();
let route = useRoute();
const projectType = getURLhandler(route.path).projectType;

// 1 编辑  0 新增 2修改
let type = ref(getURLhandler(route.path).type);

//判断是否编辑
let isUpdate = computed(() => {
	if (getURLhandler(route.path).type == 1) {
		return true;
	} else {
		return false;
	}
});
const projectRules = reactive({
	// companyId: [
	// 	{ required: true, message: "请选择实施主体!", trigger: "change" },
	// ],
	/*note: [
		{
			required: formInline.projectTypeId === '1',
			message: "项目简介不能为空!",
			trigger: "blur",
		},
	],
	industryTypeId: [
		{ required: true, message: "请选择行业类型", trigger: "change" },
	],
	investmentType: [
		{ required: true, message: "请选择投资类型", trigger: "change" },
	],
	typeId: [{ required: true, message: "请选择项目类型", trigger: "change" }],
	planStatus: [
		{ required: true, message: "请选择计划状态", trigger: "change" },
	],
	investSource: [
		{ required: true, message: "请选择投资来源", trigger: "change" },
	],
	investContent: [
		{ required: true, message: "请选择投资内容", trigger: "change" },
	],*/
	/*  investUse: [
    { required: true, message: '请选择投资用途', trigger: 'change' }
  ],*/
/*	projectAddType: [
		{ required: true, message: "请选择项目新增类型", trigger: "change" },
	],
	yearAmount: [{ required: true, message: "项目投资金额", trigger: "blur" }],
	 projectNature: [
    { required: true, message: '请选择项目性质', trigger: 'change' }
  ],*/
	projectSetId: [
		{ required: true, message: "关联项目集不能为空", trigger: "change" },
	],
	projectName: [
		{ required: true, message: "项目名称不能为空", trigger: "blur" },
	],
/*	projectCode: [
		{ required: true, message: "项目编码不能为空", trigger: "blur" },
	],*/
	projectTypeId: [
		{ required: true, message: "项目分类不能为空", trigger: "change" },
	],
	/*businessUnit: [
		{ required: true, message: "业务单元不能为空", trigger: "change" },
	],*/
	/*  streetCode: [
    { required: true, message: '项目位置不能为空', trigger: 'blur' }
  ],*/
	/*address: [
    { required: true, message: '项目详细地址不能为空', trigger: 'blur' }
  ],*/
	/*projectAmount: [
		{ required: true, message: "拟计划总投额不能为空", trigger: "blur" }
	],
	typeOfBusiness: [
		{ required: true, message: "所属业态不能为空", trigger: "change" },
	],
	time: [
		{ required: true, message: "计划实施时间范围不能为空", trigger: "blur" },
	],*/
	// responsibleDepartmentId: [
	// 	{ required: true, message: "项目立项部门不能为空", trigger: "change" },
	// ],
	/*responsibleUserId: [
		{ required: true, message: "项目负责人不能为空", trigger: "change" },
	],*/
});
const mapdialog = ref(null);
const mapclick = () => {
	if (mapdialog?.value) {
		mapdialog.value.openMap();
	}
};

const getMapAddrInfo = (data) => {
	console.log(data);
	formInline.address = data.address;
};

// 获取所有项目集
const getProjectSets = () => {
	fundManagementApi.getProjectSets().then((res) => {
		const { code, data, msg } = res;
		if (code == 200) {
			state.projectSetsList = data;
		}
	});
};

/**
 * 根据部门获取项目集
 */
const getDepartmentProjectSet = () => {
	let params = {
		departmentId: localGet('DepartmentId')
	}
	fundManagementApi.getDepartmentProjectSet(params).then((res) => {
		const { code, data, msg } = res
		if (code === '200') {
			state.projectSetsList = data
		}
	})
}


// 获取所有部门
const getAllDepartments = () => {
	department.getAllDepartments().then((res) => {
		const { code, data, msg } = res;
		if (code == 200) {
			state.allDepartmentList = data;
		}
	});
};

// 获取项目 负责人
const getLeaderByDepartmentId = (responsibleDepartmentId) => {
	/*department.getLeaderByDepartmentId(responsibleDepartmentId).then((res) => {
    const { code, data, msg } = res
    if (code == 200) {
      state.departmentPersonList = data
    }
  }) */
	department.getUserByDepartmentId(responsibleDepartmentId).then((res) => {
		const { code, data, msg } = res;
		if (code == 200) {
			state.departmentPersonList = data;
		}
	});
};

// 获取项目 执行人员
// const getUserByDepartmentId = (responsibleDepartmentId) => {
// 	department.getUserByDepartmentId(responsibleDepartmentId).then((res) => {
// 		const { code, data, msg } = res;
// 		if (code == 200) {
// 			state.userByDepartmentList = data;
// 		}
// 	});
// };
// 获取项目类型
const getProjectTypeVo = () => {
	publicProject.getProjectTypeVo().then((res) => {
		const { code, data, msg } = res;
		if (code == 200) {
			state.projectTypeList = data;
		}
	});
};

const getStageTimeChange = (e) => {
	state.startTime = e.predictStartTime;
	state.endTime = e.predictEndTime;
}

//获取所有项目信息
const projectListApiHandler = () => {
	//新增查询
	//编辑查询
	let obj = {
		projectSetId: "",
	};
	if (isUpdate) {
		obj = {
			projectSetId: getURLhandler(route.path).edit || "",
		};
	}

	projectListApi.getAllList(obj.projectSetId || "").then((res) => {
		const { code, data, msg } = res;
		if (code == 200) {
			state.projectOptionsArr = data;
		}
	});
};

const departmentIdChange = (id) => {
	formInline.responsibleUserId = "";
};

//位置编号
const streerChange = (e) => {
	state.streetList.forEach((item) => {
		if (item.value == e) {
			formInline.streetName = item.label;
		}
	});
};

// 监听 项目负责部门 调用 项目负责人
watch(
	() => formInline.responsibleDepartmentId,
	(value) => {
		if (value) {
			getLeaderByDepartmentId(value);
		}
	}
);

// 获取项目集
const getpublicProject = () => {
	projectConfigApi.getProjectSets().then((res) => {
		if (res.code == 200) {
			state.projectSetIdList = res.data.map((item) => {
				return {
					label: item.projectSetName,
					value: item.projectSetId,
				};
			});
		}
	});
};
const getTypeList = (e) => {
	const type = "10" + e;
	projectConfigApi.getTypeList(type).then((res) => {
		if (res.code == 200) {
			state.typeList = res.data.map((item) => {
				return {
					label: item.typeName,
					value: item.projectTypeId,
				};
			});
		}
	});
};

const handelChangeTime = (e) => {
	formInline.predictStartTime = '';
	formInline.predictEndTime = '';
	state.startTime = '';
	state.endTime = '';
	if (e && e.length) {
		formInline.predictStartTime = e[0];
		formInline.predictEndTime = e[1];
		state.startTime = e[0];
		state.endTime = e[1];
	}

};

//提交
const submitHandler = (formEl) => {
	if(formInline.projectAmount < formInline.yearAmount){
		formInline.projectAmount = '';
		return ElMessage.error('年度计划投资金额不能大于拟计划总投额!');
	}

	if (formEl) {
		formEl.validate((valid, fields) => {
			if (valid) {
				if (getURLhandler(route.path).type == 0 && state.active == 1) {
					// formInline.companyId = "高新社事投";
					projectConfigApi.addOne(formInline).then((res) => {
						const { code, data, msg } = res;
						if (code == 200) {
							state.success = true;
							state.active = 2;
							ElMessage.success("新增成功");
							state.projectId = data;
						}
					});
				}

				if (getURLhandler(route.path).type == 1 && state.active == 1) {
					// formInline.companyId = "高新社事投";
					projectConfigApi.addOne(formInline).then((res) => {
						const { code, data, msg } = res;
						if (code == 200) {
							state.success = true;
							state.active = 2;
							ElMessage.success("编辑成功");
							state.projectId = data;
						}
					});
				}

				if (state.active == 2) {
					if (projectStagetree.value.state.dataSource?.length === 0) {
						ElMessage.warning("请添加项目阶段");
						return;
					}

					// 第二步
					projectStagetree.value.createStage(() => {
						state.active = 3;
						ElMessage.success("操作成功");
					});
				}
			}
		});
	}
	if (!formEl) {
		if (getURLhandler(route.path).type == 0 && state.active == 1) {
			// formInline.companyId = "高新社事投";
			projectConfigApi.addOne(formInline).then((res) => {
				const { code, data, msg } = res;
				if (code == 200) {
					state.success = true;
					state.active = 2;
					ElMessage.success("新增成功");
					state.projectId = data;
				}
			});
		}

		if (getURLhandler(route.path).type == 1 && state.active == 1) {
			// formInline.companyId = "高新社事投";
			projectConfigApi.addOne(formInline).then((res) => {
				const { code, data, msg } = res;
				if (code == 200) {
					state.success = true;
					state.active = 2;
					ElMessage.success("编辑成功");
					state.projectId = data;
				}
			});
		}

		if (state.active == 2) {
			if (projectStagetree.value.state.dataSource?.length === 0) {
				ElMessage.warning("请添加项目阶段");
				return;
			}

			// 第二步
			projectStagetree.value.createStage(() => {
				state.active = 3;
				ElMessage.success("操作成功");
			});
		}
	}
};


/*const submitHandler = (formEl) => {
    // 第一步
    if (formEl) {
        formEl.validate((valid, fields) => {
            if (valid) {
                if (getURLhandler(route.path).type == 0) {
                    projectConfigApi.prepareCreate(formInline).then((res) => {
                        const { code, data, msg } = res
                        if (code === '200') {
                            state.success = true
                            state.active = 2
                            ElMessage.success('新增成功')
                            state.projectId = data
                        }
                    })
                } else {
                    //编辑提交
                    projectConfigApi.prepareUpdate(formInline).then((res) => {
                        const { code, data, msg } = res
                        if (code === '200') {
                            state.success = true
                            state.active = 2
                            ElMessage.success('编辑成功')
                        }
                    })
                }
            }
        })
    }

    if(!formEl) {

        if(getURLhandler(route.path).type == 0 && state.active==1){
            projectConfigApi.prepareCreate(formInline).then((res) => {
                const { code, data, msg } = res
                if (code === '200') {
                    state.success = true
                    state.active = 2
                    ElMessage.success('新增成功')
                    state.projectId = data
                }
            })
        }

        if(getURLhandler(route.path).type == 1 && state.active==1){
            //编辑提交
            projectConfigApi.prepareUpdate(formInline).then((res) => {
                const { code, data, msg } = res
                if (code === '200') {
                    state.success = true
                    state.active = 2
                    ElMessage.success('编辑成功')
                }
            })
        }


        if(state.active==2){
            if (projectStagetree.value.state.dataSource?.length === 0) {
                ElMessage.warning('请添加项目阶段')
                return
            }

            // 第二步
            projectStagetree.value.createStage(()=>{
                state.active = 3
                ElMessage.success("操作成功");
            });
        }
    }
}*/

//返回列表页
const goback = () => {
	router.push({
		path: "/constructionProjectManage/preliminaryProject/index",
	});
};

/**
 * 通过ID获取详情
 * @param id
 */
const getByidHandler = (id) => {
	/* projectListApi.getprojectByid(id).then((res) => {
    const { code, data, msg } = res
    if (code === '200') {
      Object.assign(formInline, data)
      formInline.time = [data.predictStartTime, data.predictEndTime]
    }
  })*/
	investmentProjects.getInfo({ projectId: id }).then((res) => {
		const { code, data, msg } = res;
		if (code === "200") {
			Object.assign(formInline, data);
			formInline.time = [data.predictStartTime, data.predictEndTime];
			console.log("formInline", formInline.time);
		}
	});
};
const initAreaOptions = (type = "2", cd = "") => {
	// type：1-获取区域 ；2-获取街道
	// cd：获取区域时不传
	assetsApi.getLocations(type, cd).then((res) => {
		const { code, data, msg } = res;
		if (code == 200) {
			res.data.forEach((item) => {
				item.label = item.name;
				item.value = item.code;
			});
			state.streetList = res.data;
		}
	});
};

/**
 * 行业类型
 */
const getListEnum = () => {
	otherApi.getListEnum({ type: 2 }).then((res) => {
		if (res.code === "200") {
			state.industryTypeIdList = res.data;
		}
	});
};

/**
 * 公司列表
 */
const getCompanyList = () => {
	otherApi.getCompanyList({}).then((res) => {
		if (res.code === "200") {
			state.companyList = res.data;
		}
	});
};

/**
 * 获取业务单元
 */
const businessUnitList = ref([]);
const getFundBudgetBusinessUnitAll = () => {
	fundManagementApi.getFundBudgetBusinessUnitAll().then((res) => {
		if (res.code === "200") {
			businessUnitList.value = res.data;
		}
	});
};

defineExpose({
	centerDialogVisible,
	state,
});
onMounted(() => {
	getListEnum();
	initAreaOptions();
	formInline.type = getURLhandler(route.path)?.projectType;
	projectListApiHandler();
	getAllDepartments();
	// getProjectSets();
	getDepartmentProjectSet();
	getpublicProject();
	getTypeList(getURLhandler(route.path)?.projectType);
	getProjectTypeVo();
	getCompanyList();
	getFundBudgetBusinessUnitAll();

	if (getURLhandler(route.path).status !== undefined) {
		state.active = getURLhandler(route.path).status;
	}
	if (getURLhandler(route.path).type != "0") {
		//编辑
		getByidHandler(getURLhandler(route.path)?.projectId || "");
	} else {
		formInline.responsibleDepartmentId = JSON.parse(
			window.localStorage.getItem("SST-DepartmentId")
		);
	}
});

let allType = [
	{
		label: "建设",
		value: 1,
	},
	{
		label: "装修",
		value: 2,
	},
	{
		label: "设备",
		value: 3,
	},
	{
		label: "新设",
		value: 4,
	},
	{
		label: "收购",
		value: 5,
	},
	{
		label: "增资",
		value: 6,
	},
	{
		label: "其他",
		value: 7,
	},
];
let activeArr1 = allType.filter((item) => {
	let arr = [4, 5, 6, 7];
	return arr.indexOf(item.value) > -1;
});
let activeArr2 = allType.filter((item) => {
	let arr = [1, 2, 3, 7];
	return arr.indexOf(item.value) > -1;
});
let selectionArr = ref([]);
selectionArr.value = allType;
let projectTypeChange = (e) => {
	if (e === "1") {
		selectionArr.value = activeArr1;
	} else if (e === "2") {
		selectionArr.value = activeArr2;
	} else {
		selectionArr.value = allType;
  }
  if (formInline.investContent) {
    let i = selectionArr.value.findIndex((item) => {
      return item.value === formInline.investContent;
    });
    if (i < 0) {
      formInline.investContent = selectionArr.value[0].value;
    }
  }
};
</script>
<style lang="scss" scoped>
.demo-form-inline {
	margin-top: 32px;
	margin-bottom: 20px;
	border: 1px solid #e3e7f1;
	padding: 15px;
	border-radius: 10px;
}
.el-select {
	width: 50%;
}
.select_project {
	width: 100%;
}
.buttons {
	text-align: center;
}
.twoline {
	display: flex;
	.el-form-item {
		width: 100%;
		display: flex;
		justify-content: space-between;
		.el-select {
			width: 100%;
		}
	}

	justify-content: space-between;
}
.oneline {
	.el-form-item {
		width: 100%;
	}
}
</style>
<style lang="scss" scoped>
.el-step__title.is-process,
is-text {
	font-weight: normal !important;
	color: #a8abb2 !important;
}
.is-process {
	border-color: #a8abb2 !important;
}
.el-step__head.is-process {
	font-weight: normal !important;
	color: #a8abb2 !important;
}
.public-dialog-content {
	:deep(.el-form-item__content) {
		width: 100%;
	}
	.el-form {
		width: 400px;
	}
	.el-input,
	.el-select {
		width: 100%;
	}
}
.el-dialog__body .el-form .el-form-item {
	width: 100% !important;
}

.el-select {
	width: 100% !important;
}
</style>
